package com.cxb;

import android.content.Intent;
import android.os.Bundle;
import android.text.InputFilter;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnLongClickListener;
import android.view.View.OnTouchListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.cxb.utils.CommonUtils;
import com.cxb.utils.SharedPreferencesUtils;
import com.cxb.view.TTSToast;
import com.cxb.R;


public class SettingNumberActivity extends BaseActivity implements
		View.OnClickListener {
	protected TextView tvPhoneName;
	protected EditText etPhoneNumber;
	protected Button btnDel, btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8,
			btn9, btn0;
	protected Button btBack,btSave;
	protected SharedPreferencesUtils sp;
	protected String sPhoneNumber;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
//		this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
		setContentView(R.layout.activity_setting_number);
		init();
		sp = new SharedPreferencesUtils(this);
//		tvPhoneName.setText(R.string.number_mobile);
		sPhoneNumber = sp.getLocalNumber();
		etPhoneNumber.setText(sPhoneNumber);
		etPhoneNumber.setFilters(new InputFilter[] { new InputFilter.LengthFilter(11) });
		etPhoneNumber.setSelection(etPhoneNumber.getText().toString().length());
	}

	private void init() {
		tvPhoneName = (TextView) findViewById(R.id.tv_phonename);
		etPhoneNumber = (EditText) findViewById(R.id.et_phonenumber);
		btn1 = (Button) findViewById(R.id.bt_1);
		btn2 = (Button) findViewById(R.id.bt_2);
		btn3 = (Button) findViewById(R.id.bt_3);
		btn4 = (Button) findViewById(R.id.bt_4);
		btn5 = (Button) findViewById(R.id.bt_5);
		btn6 = (Button) findViewById(R.id.bt_6);
		btn7 = (Button) findViewById(R.id.bt_7);
		btn8 = (Button) findViewById(R.id.bt_8);
		btn9 = (Button) findViewById(R.id.bt_9);
		btn0 = (Button) findViewById(R.id.bt_0);
		btnDel = (Button) findViewById(R.id.bt_del);
		btBack = (Button) findViewById(R.id.bt_back);
		btSave = (Button) findViewById(R.id.bt_save);
//		btSkip = (Button) findViewById(R.id.bt_skip);
		btn1.setOnClickListener(this);
		btn2.setOnClickListener(this);
		btn3.setOnClickListener(this);
		btn4.setOnClickListener(this);
		btn5.setOnClickListener(this);
		btn6.setOnClickListener(this);
		btn7.setOnClickListener(this);
		btn8.setOnClickListener(this);
		btn9.setOnClickListener(this);
		btn0.setOnClickListener(this);
		btnDel.setOnClickListener(this);
		btnDel.setOnLongClickListener(new OnLongClickListener() {

			@Override
			public boolean onLongClick(View v) {
				// TODO Auto-generated method stub
				etPhoneNumber.setText("");
				return false;
			}
		});

		btBack.setOnClickListener(this);
		btSave.setOnClickListener(this);
		etPhoneNumber.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				etPhoneNumber.onTouchEvent(event);
				InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
				imm.hideSoftInputFromWindow(etPhoneNumber.getWindowToken(), 0);

				return true;
			}
		});

	}

	public void onClick(View v) {
		int index = etPhoneNumber.getSelectionStart();
		switch (v.getId()) {
		case R.id.bt_1:
			editPhoneNumber(index, "1");
			break;
		case R.id.bt_2:
			editPhoneNumber(index, "2");
			break;
		case R.id.bt_3:
			editPhoneNumber(index, "3");
			break;
		case R.id.bt_4:
			editPhoneNumber(index, "4");
			break;
		case R.id.bt_5:
			editPhoneNumber(index, "5");
			break;
		case R.id.bt_6:
			editPhoneNumber(index, "6");
			break;
		case R.id.bt_7:
			editPhoneNumber(index, "7");
			break;
		case R.id.bt_8:
			editPhoneNumber(index, "8");
			break;
		case R.id.bt_9:
			editPhoneNumber(index, "9");
			break;
		case R.id.bt_0:
			editPhoneNumber(index, "0");
			break;
		case R.id.bt_del:
			if (index > 0) {
				etPhoneNumber.setText(etPhoneNumber.getText().delete(index - 1,
						index));
				etPhoneNumber.setSelection(index - 1);
			}
			break;
		case R.id.bt_save:
			save();
			break;
		case R.id.bt_back:
			finish();
			break;
			
		default:
			break;
		}
	}

	private void editPhoneNumber(int index, String s) {
		if (etPhoneNumber.getText().toString().length() < 11) {
			etPhoneNumber.setText(etPhoneNumber.getText().insert(index, s));
			etPhoneNumber.setSelection(index + 1);
		}

	}

	protected void save() {
		// TODO Auto-generated method stub
		if (CommonUtils.isMobileNO(etPhoneNumber.getText().toString())) {
			
			sp.setLocalNumber(etPhoneNumber.getText().toString());
			TTSToast.show(getApplicationContext(),
					getString(R.string.save_success), Toast.LENGTH_SHORT);
		} else {
			TTSToast.show(getApplicationContext(),
					getString(R.string.input_error), Toast.LENGTH_SHORT);
		}
	}
}
